﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;
using HRM.DAL.Report;

namespace HRM.UI.HRAdminUI.EmpModule
{
    public partial class EditEmployeePersonal : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDesignation();
                LoadProject();
                Loadcatagory();


            }

        }

        private void LoadDesignation()
        {
            List<Designation> designations = new List<Designation>();
            DesignationManager manager = new DesignationManager();
            designations = manager.GetAllDesignations();
            ddlEmpdesignation.DataTextField = "Title";
            ddlEmpdesignation.DataValueField = "Id";
            ddlEmpdesignation.DataSource = designations;
            ddlEmpdesignation.DataBind();
            ddlEmpdesignation.Items.Insert(0, "---Choose Any---");
        }

        private void LoadProject()
        {
            List<Project> projects = new List<Project>();
            ProjectManager manager = new ProjectManager();
            projects = manager.GetAllProjects();
            ddlEmpProject.DataTextField = "ProjectName";
            ddlEmpProject.DataValueField = "Id";
            ddlEmpProject.DataSource = projects;
            ddlEmpProject.DataBind();
            ddlEmpProject.Items.Insert(0, "---Choose Any---");

        }

        private void Loadcatagory()
        {
            List<Catagory> catagories = new List<Catagory>();
            CatagoryManager manager = new CatagoryManager();
            catagories = manager.GetAllCatagories();
            ddempCatagory.DataTextField = "EmpCatagory";
            ddempCatagory.DataValueField = "Id";
            ddempCatagory.DataSource = catagories;
            ddempCatagory.DataBind();
            ddempCatagory.Items.Insert(0, "---Choose Any---");

        }
        private void LoadStatus()
        {

            List<Status> statuses = new List<Status>();
            StatusManager manager = new StatusManager();
            statuses = manager.GetAllStatus();
            ddStatus.DataTextField = "EmpStatus";
            ddStatus.DataValueField = "Id";
            ddStatus.DataSource = statuses;
            ddStatus.DataBind();
            ddStatus.Items.Insert(0, "---Choose Any---");

        }




        protected void SearchEmployee(object sender, EventArgs e)
        {

            try
            {
                EmployeeManager manager = new EmployeeManager();
                Code code = new Code();
                code.CodeName = txtEmpCode.Value;
                ViewEmployee viewEmployee = new ViewEmployee();
                viewEmployee = manager.SearchEmployeeByCode(code);
                viewEmployee.Employee.EmpCode = code;

                if (viewEmployee.Employee.Name != null)
                {
                    employeeCode.Value = code.CodeName;
                    txtName.Value = viewEmployee.Employee.Name;
                    txtFname.Value = viewEmployee.Employee.FathersName;
                    txtMname.Value = viewEmployee.Employee.MothersName;
                    ddlsex.Value = viewEmployee.Employee.Sex;
                    ddbloodgroup.Value = viewEmployee.Employee.Blood;
                    txtDob.Value = (viewEmployee.Employee.DOB).ToShortDateString();
                    txtEmail.Value = viewEmployee.Employee.Email;
                    txtnationality.Value = viewEmployee.Employee.Nationality;
                    txtNationalID.Value = viewEmployee.Employee.NID;
                    txtSkill.Value = viewEmployee.Employee.Skill;
                    txtJoiningDate.Value = (viewEmployee.Employee.JoiningDate).ToShortDateString();
                    txtContatFinishDate.Value = (viewEmployee.Employee.ContractFinishDate).ToShortDateString();
                    ddStatus.Value = viewEmployee.Employee.Status;
                    txtEducation.Value = viewEmployee.Employee.Education;
                    ViewState["resume"] = viewEmployee.Employee.Resume;

                    desigPara.Text = viewEmployee.Designation.Title;
                    projectPara.Text = viewEmployee.Project.ProjectName;
                    catagoryPara.Text = viewEmployee.Catagory.EmpCatagory;
                    msgLabel.Visible = false;
                    desigPara.ForeColor = Color.Red;
                    projectPara.ForeColor = Color.Red;
                    catagoryPara.ForeColor = Color.Red;

                }


                else
                {
                    msgLabel.Visible = true;
                    msgLabel.Text = "Employee Data not Found For Editing";
                    msgLabel.ForeColor = Color.Red;


                }

            }
            catch (Exception)
            {

                ;
            }

        }

        protected void UpdatingEmployee(object sender, EventArgs e)
        {
            try
            {
                bool ok = true;
                Employee employee = new Employee();
                EmployeeManager manager = new EmployeeManager();

                Code code = new Code();
                Designation designation = new Designation();
                Project project = new Project();
                Catagory catagory = new Catagory();

                employee.Name = txtName.Value;
                employee.FathersName = txtFname.Value;
                employee.MothersName = txtMname.Value;
                employee.Sex = ddlsex.Value;
                employee.Blood = ddbloodgroup.Value;
                employee.DOB = Convert.ToDateTime(txtDob.Value);
                employee.Email = txtEmail.Value;
                employee.Nationality = txtnationality.Value;
                employee.NID = txtNationalID.Value;

                if (ddlEmpdesignation.Value == "---Choose Any---")
                {
                    msgLabel.Text = "Please Select a Designation";
                    ok = false;
                }
                else
                {
                    designation.Id = Convert.ToInt32(ddlEmpdesignation.Value);
                    employee.Designation = designation;
                }
                employee.Skill = txtSkill.Value;
                employee.JoiningDate = Convert.ToDateTime(txtJoiningDate.Value);
                if (txtContatFinishDate.Value != "")
                {
                    employee.ContractFinishDate = Convert.ToDateTime(txtContatFinishDate.Value);

                }
                else
                {
                    employee.ContractFinishDate = Convert.ToDateTime("01-01-1800");
                }

                if (ddlEmpProject.Value == "---Choose Any---")
                {
                    msgLabel.Text = "Please Select a Project";
                    ok = false;
                }
                else
                {
                    project.Id = Convert.ToInt32(ddlEmpProject.Value);
                    employee.Project = project;
                }

                if (ddempCatagory.Value == "---Choose Any---")
                {
                    msgLabel.Text = "Please Select a Catagory";
                    ok = false;
                }
                else
                {
                    catagory.Id = Convert.ToInt32(ddempCatagory.Value);
                    employee.EmpCatagoryId = catagory;
                }
                employee.Status = ddStatus.Value;
                employee.Education = txtEducation.Value;
                code.CodeName = employeeCode.Value;
                employee.EmpCode = code;
                string resumeName = Path.GetFileName(uploadresume.PostedFile.FileName);
                uploadresume.PostedFile.SaveAs(Server.MapPath("~/Resume/" + code.CodeName + "_" + resumeName));
                if (resumeName == "")
                {
                    employee.Resume = ViewState["resume"].ToString();
                }
                else
                {
                    employee.Resume = code.CodeName + "_" + resumeName;
                }
                if (ok)
                {
                    if (manager.EditEmployeeInfo(employee))
                    {
                        ClearInput();
                        msgLabel.ForeColor = Color.Green;
                        msgLabel.Text = "Employee Updated Successfully !";

                    }
                    else
                    {
                        msgLabel.ForeColor = Color.Red;
                        msgLabel.Text = "Employee Not Updated !";
                    }
                }
                else
                {
                    msgLabel.Visible = true;
                    msgLabel.ForeColor = Color.Red;
                }

            }
            catch (Exception ex)
            {
                msgLabel.ForeColor = Color.Red;
                msgLabel.Text = ex.Message;
            }
        }
        protected void ClearInput()
        {
            txtName.Value = "";
            txtContatFinishDate.Value = "";
            txtDob.Value = "";
            txtEducation.Value = "";
            txtEmail.Value = "";
            txtFname.Value = "";
            txtMname.Value = "";
            txtNationalID.Value = "";
            txtSkill.Value = "";
            txtnationality.Value = "";
            ddStatus.Value = "";
            ddbloodgroup.Value = "";
            ddempCatagory.Value = "";
            ddlEmpProject.Value = "";
            ddlsex.Value = "";
            txtJoiningDate.Value = "";
        }

    }
}